$(document).ready(function () {
    $("#closeDiv").click(function() {
        $("#div_detail").css('display','none');
    })
    $("#checkLocation").change(function() {
        var result=$("#checkLocation").val();
        if(result==1) {
            $("#chooseLocation").css('display','block');
        }
        else{
            $("#chooseLocation").css('display','none');
        }
    });
    $("#checkLocation1").change(function() {
        var result=$("#checkLocation1").val();
        if(result==1) {
            $("#chooseLocation1").css('display','block');
        }
        else{
            $("#chooseLocation1").css('display','none');
        }
    });
    init();
    initSelect();

    function initSelect() {
        $.ajax({
            type:"get",
            url:"/location/getAll",
            dataType:"json",
            success:function(data) {
                for (var i in data) {
                    $("#places").append("<li><input type='checkbox' value='" + data[i].id + "' name='locations'/>" + data[i].name+"</li>");
                }
            }
        });
    };
    function initSelect1(locations) {
        $.ajax({
            type:"get",
            url:"/location/getAll",
            dataType:"json",
            success:function(data) {
                for (var i in data) {
                    $("#places1").append("<li><input type='checkbox' class='locations1' value='" + data[i].id + "' name='locations1'/>" + data[i].name + "</li>");
                }
                var places1 = $(".locations1");
                for(var i=0;i<places1.length;i++){
                    for(var j=0;j<locations.length;j++){
                        if(places1[i].value == locations[j]){
                            places1[i].checked = true;
                            break;
                        }
                    }
                }
            }
        });
    };

    $("#b_update").click(function() {
        var id=$("#text_id").val();
        var name=$("#text_name").val();
        var money=$("#text_money").val();
        var startTime=$("#text_startTime").val();
        var endTime=$("#text_endTime").val();
        var checkLocation=$("#checkLocation1").val();
        var locations="";
        $('input[name="locations1"]:checked').each(function(i){
            if(0==i){
                locations = $(this).val();
            }else{
                locations+= (","+$(this).val());
            }
        });
        var event = {
            id:id,
            name: name,
            money: money,
            startTime: startTime,
            endTime: endTime,
            checkLocation:checkLocation,
            locations:locations
        };
        $.ajax({
            type:"post",
            url:"/event/updateEventById",
            data:event,
            dataType:"json",
            success:function(data) {
                if(data.result=="success"){
                    alert("更新成功！");
                    init();
                }
            }
        })

    })
    $("#add_button").click(function () {
        var name = $("#name").val();
        var money = $("#money").val();
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        var checkLocation=$("#checkLocation").val();
        var locations="";
        $('input[name="locations"]:checked').each(function(i){
            if(0==i){
                locations = $(this).val();
            }else{
                locations+= (","+$(this).val());
            }
        });



        var event = {
            name: name,
            money: money,
            startTime: startTime,
            endTime: endTime,
            checkLocation:checkLocation,
            locations:locations
        };
        $.ajax({
            type: "post",
            url: "/event/addEvent",
            data: event,
            dataType: "json",
            success: function changePassword1(data) {
                if(data.result=="success"){
                    alert("添加成功！！！");
                    window.location.href = "/events";

                }
            },
            error: function (error) {

            }
        });
    })


    function init() {
        $("#t1 tr:not(:first)").empty();
        //clearTable(t1);
        $.ajax({
            type: "GET",
            url: "/event/allEvent",
            dateType: "json",
            success: function (data) {
                if (data != null || data != "") {
                    var table = $("#t1");
                    for (var i in data) {
                        table.append("<tr><td>" + data[i].id + "</td><td>" + data[i].name + "</td><td>" + data[i].money + "</td><td>" + data[i].startTime + "</td><td>" + data[i].endTime + "</td><td>" + data[i].checkLocation + "</td><td><input type='button' class='b_delete' value='删除' /></td><td><input type='button' class='b_detail' value='详情' /></td></tr>")
                    }
                }
                $(".b_delete").click(function() {
                    var id = $(this).parent().parent().find("td").eq(0).text();
                    var event={
                        id:id
                    };
                    if(confirm("确认是否删除？")){
                        $.ajax({
                            type:"post",
                            url:"/event/deleteEventById",
                            data:event,
                            dataType:"json",
                            success:function(data) {
                                if(data.result=="success"){
                                    alert("删除成功！");
                                    init();
                                }
                            }
                        })
                    }
                });

                $(".b_detail").click(function() {
                    $("#div_detail").css('display','block');
                    var id = $(this).parent().parent().find("td").eq(0).text();
                    var event={
                        id:id
                    };
                    $.ajax({
                        type:"post",
                        url:"/event/getEventById",
                        data:event,
                        dataType:"json",
                        success:function(data) {
                            $("#text_id").val(data.id);
                            $("#text_name").val(data.name);
                            $("#text_money").val(data.money);
                            $("#text_startTime").val(data.startTime);
                            $("#text_endTime").val(data.endTime);
                            $("#checkLocation1").val(data.checkLocation);
                            var locations=(data.locations).split(",");
                            $("input[name='locations1']:checkbox").each(function(){
                                $(this).parent('li').remove();
                            });
                            initSelect1(locations);

                            if(data.checkLocation==1){
                                $("#chooseLocation1").css('display','block');
                            }
                            else{
                                $("#chooseLocation1").css('display','none');
                            }
                            $("#text_name").focus();
                        }
                    });
                });

            }

        })
    }

});